//
//  HRSearchToolView.m
//  Hear
//
//  Created by mac on 2017/8/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRSearchToolView.h"
#import <CoreLocation/CoreLocation.h>
#import "YBAlertView.h"
#import "JLDoubleSlider.h"
@interface HRSearchToolView ()
@property (nonatomic, copy)SelectEnterBlock selectEnterBlock;
@property(nonatomic, strong)UIButton *tempSexButton;
@property(nonatomic, strong)UIButton *tempSortButton;
@property(nonatomic, strong)UIButton *tempLikeButton;

@end
@implementation HRSearchToolView
+(void)showSearchToolView:(SelectEnterBlock)selectEnterBlock{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRSearchToolView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRSearchToolView class]]){
        
        
        HRSearchToolView *shareView = (HRSearchToolView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadView:selectEnterBlock];
        
    }
}

- (void)loadView:(SelectEnterBlock)selectEnterBlock{
    _selectEnterBlock = selectEnterBlock;
    
    UIButton *closeButton = [UIButton buttonWithType:0];
    closeButton.frame = self.bounds;
    [closeButton addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:closeButton];
    
    
    self.alpha = 0;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kTopHeight, kFullWidth, 300)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    contentView.alpha = 0;
    
    self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1;
        contentView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
    
    
    CGFloat pading = 20;

    CGFloat buttonHeight = 35;
    
    
    UILabel *likeSortLabel = [UILabel new];
    likeSortLabel.text = @"排序";
    likeSortLabel.font = CustomUIFont(14);
    likeSortLabel.textColor =FirstTextColor;
    [contentView  addSubview:likeSortLabel];
    likeSortLabel.frame = CGRectMake(pading, pading, 40, buttonHeight);
    
    CGFloat buttonWidth = (kFullWidth-likeSortLabel.right-pading-pading*3)/3;

    
    NSArray *likeArr = @[@"活跃优先",@"点赞优先"];
    for (int i=0; i <likeArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:0];
        [button setTitle:likeArr[i] forState:0];
        [button setTitleColor:FirstTextColor forState:0];
        button.titleLabel.font = CustomUIFont(14);
        [button addTarget:self action:@selector(likeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:button];
        button.tag = i+1;
        button.frame = CGRectMake(likeSortLabel.right+pading+i*(buttonWidth+pading), likeSortLabel.top, buttonWidth, buttonHeight);
        button.layer.borderColor = CustomColor(240, 240, 240, 1).CGColor;
        button.layer.borderWidth = 1;
        
        NSInteger currentSelectIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"hear_selectSortType"];
        
        int showIndex = 1;
        if (currentSelectIndex==1){
            showIndex = 0;
        }else if (currentSelectIndex == 2){
            
        }else if (currentSelectIndex ==3){
            showIndex = 1;
        }
        if (i ==showIndex){
            button.backgroundColor = CustomColor(240, 240, 240, 1);
            self.tempLikeButton = button;
        }
    }
    UILabel *lineLabel1 = [UILabel new];
    lineLabel1.backgroundColor = CustomColor(240, 240, 240, 1);
    [contentView  addSubview:lineLabel1];
    lineLabel1.frame = CGRectMake(contentView.left, likeSortLabel.bottom+pading, contentView.width, 1);
    
    
    UILabel *sexLabel = [UILabel new];
    sexLabel.text = @"性别";
    sexLabel.font = CustomUIFont(14);
    sexLabel.textColor = FirstTextColor;
    [contentView  addSubview:sexLabel];
    sexLabel.frame = CGRectMake(pading, lineLabel1.bottom+pading, 40, buttonHeight);
    
    NSArray *sexArr = @[@"不限",@"男",@"女"];
    NSArray *sexImageArr = @[@"",@"",@""];
    
    
    for (int i =0; i <sexArr.count; i ++) {
        UIButton *button = [UIButton buttonWithType:0];
        [button setTitle:sexArr[i] forState:0];
        [button setImage:[UIImage imageNamed:sexImageArr[i]] forState:0];
        [button setTitleColor:FirstTextColor forState:0];
        button.titleLabel.font = CustomUIFont(14);
        [button addTarget:self action:@selector(sexButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:button];
        button.tag = i+1;
        button.frame = CGRectMake(sexLabel.right+pading+i*(buttonWidth+pading), sexLabel.top, buttonWidth, buttonHeight);
        button.layer.borderColor = CustomColor(240, 240, 240, 1).CGColor;
        button.layer.borderWidth = 1;
        

        
        if (i == ([[NSUserDefaults standardUserDefaults] integerForKey:@"hear_selectSex"] -1)){
            button.backgroundColor = CustomColor(240, 240, 240, 1);
            self.tempSexButton = button;
        }
        
    }
    
    
    UILabel *lineLabel = [UILabel new];
    lineLabel.backgroundColor = CustomColor(240, 240, 240, 1);
    [contentView  addSubview:lineLabel];
    lineLabel.frame = CGRectMake(contentView.left, sexLabel.bottom+pading, contentView.width, 1);
    
    
    UILabel *sortLabel = [UILabel new];
    sortLabel.text = @"年龄";
    sortLabel.font = CustomUIFont(14);
    sortLabel.textColor =FirstTextColor;
    [contentView  addSubview:sortLabel];
    sortLabel.frame = CGRectMake(sexLabel.left, lineLabel.bottom+pading, sexLabel.width, sexLabel.height);
    
    
    UILabel *minAgeLabel = [UILabel new];
    minAgeLabel.text = @"18";
    minAgeLabel.font = CustomUIFont(14);
    minAgeLabel.textColor = FirstTextColor;
    [contentView  addSubview:minAgeLabel];
    minAgeLabel.frame = CGRectMake(sortLabel.right, sortLabel.top+2, 30, sortLabel.height);
    
    
    CGFloat sliderWidth = contentView.width-minAgeLabel.right*2;
    UILabel *maxAgeLabel = [UILabel new];
    maxAgeLabel.text = @"50+";
    maxAgeLabel.font = CustomUIFont(14);
    maxAgeLabel.textColor = FirstTextColor;
    [contentView  addSubview:maxAgeLabel];
    maxAgeLabel.frame = CGRectMake(contentView.width-minAgeLabel.right+10, minAgeLabel.top, minAgeLabel.width, sortLabel.height);
    
    JLDoubleSlider *slider = [[JLDoubleSlider alloc]initWithFrame:CGRectMake(minAgeLabel.right, sortLabel.centerY-buttonHeight/2, sliderWidth, buttonHeight)];
    slider.minNum = 18;
    slider.maxNum = 50;
    slider.minTintColor = CustomColor(189, 189, 189, 1);
    slider.maxTintColor = CustomColor(189, 189, 189, 1);
    slider.mainTintColor = MainColor;
    slider.currentMaxValue = [[[NSUserDefaults standardUserDefaults] objectForKey:@"currentMaxValue"] intValue];
    slider.currentMinValue = [[[NSUserDefaults standardUserDefaults] objectForKey:@"currentMinValue"] intValue];
    if (slider.currentMaxValue ==50){
        maxAgeLabel.text = @"50+";
    }else if (slider.currentMaxValue){
        maxAgeLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentMaxValue"];
    }
    if (slider.currentMinValue){
        minAgeLabel.text =[[NSUserDefaults standardUserDefaults] objectForKey:@"currentMinValue"];
    }
    
    [contentView addSubview:slider];
    slider.changeMinValueBlock = ^(NSString *value) {
        if ([value intValue]<=slider.maxNum){
//            maxAgeLabel.text = [NSString stringWithFormat:@"%d+",[value intValue]];
            [[NSUserDefaults standardUserDefaults] setObject:minAgeLabel.text forKey:@"currentMinValue"];
            minAgeLabel.text = [NSString stringWithFormat:@"%d",[value intValue]];
        }
    };
    slider.changeMaxValueBlock = ^(NSString *value) {
        if ([value intValue]<=slider.maxNum){
            maxAgeLabel.text = [NSString stringWithFormat:@"%d+",[value intValue]];
            maxAgeLabel.text = [NSString stringWithFormat:@"%d",[value intValue]];
            [[NSUserDefaults standardUserDefaults] setObject:maxAgeLabel.text forKey:@"currentMaxValue"];
        }
    };
    
    
//    NSArray *sortTextArr = @[@"最近活跃",@"距离远近"];
//    NSArray *sortImageArr = @[@"",@""];
//    for (int i = 0; i < sortTextArr.count; i ++) {
//        UIButton *button = [UIButton buttonWithType:0];
//        [button setTitle:sortTextArr[i] forState:0];
//        [button setImage:[UIImage imageNamed:sortImageArr[i]] forState:0];
//        [button setTitleColor:FirstTextColor forState:0];
//        button.titleLabel.font = CustomUIFont(14);
//        [button addTarget:self action:@selector(sortButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//        [contentView addSubview:button];
//        button.tag = i+1;
//        button.frame = CGRectMake(sortLabel.right+pading+i*(buttonWidth+pading), sortLabel.top, buttonWidth, buttonHeight);
//
//        button.layer.borderColor = CustomColor(240, 240, 240, 1).CGColor;
//        button.layer.borderWidth = 1;
//        if (i == ([[NSUserDefaults standardUserDefaults] integerForKey:@"hear_selectSortType"]-1)){
//            button.backgroundColor = CustomColor(240, 240, 240, 1);
//            self.tempSortButton = button;
//        }
//    }
    
    
    
    
    CGFloat bottomPading = 35;
    UIButton *recoveryButton = [UIButton buttonWithType:0];
    recoveryButton.frame = CGRectMake(bottomPading, sortLabel.bottom+bottomPading, (kFullWidth-bottomPading*3)/2, 45);
    [recoveryButton setTitle:@"恢复默认" forState:0];
    [recoveryButton setTitleColor:[UIColor whiteColor] forState:0];
    recoveryButton.titleLabel.font = CustomUIFont(16);
    [recoveryButton addTarget:self action:@selector(recoveryButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:recoveryButton];
    recoveryButton.backgroundColor = [UIColor lightGrayColor];
    recoveryButton.layer.cornerRadius = 7;
    recoveryButton.clipsToBounds = YES;
    
    
    UIButton *enterButton = [UIButton buttonWithType:0];
    enterButton.frame = CGRectMake(recoveryButton.right+bottomPading, recoveryButton.top, recoveryButton.width, recoveryButton.height);
    [enterButton setTitle:@"确定" forState:0];
    [enterButton setTitleColor:[UIColor whiteColor] forState:0];
    enterButton.titleLabel.font = CustomUIFont(16);
    [enterButton addTarget:self action:@selector(enterButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:enterButton];
    enterButton.backgroundColor = BlueColor;
    enterButton.layer.cornerRadius = 7;
    enterButton.clipsToBounds = YES;
    
    
}

- (void)enterButtonClick{
    NSString *sexStr = @"";


    if (self.tempSexButton.tag == 1){
        sexStr = @"2";
    }else if (self.tempSexButton.tag == 2){
        sexStr = @"1";
    }else if (self.tempSexButton.tag == 3){
        sexStr = @"0";
    }
    NSString *sortStr = @"";

    if (self.tempSortButton.tag == 1){
        sortStr = @"1";
    }else if (self.tempSortButton.tag == 2){
        sortStr = @"2";
    }else if (self.tempSortButton.tag == 3){
        sortStr = @"1";
    }
    if (self.tempLikeButton.tag == 1){
        sortStr = @"1";
    }else if (self.tempLikeButton.tag == 2){
        sortStr = @"3";
    }
    
    if(self.tempSortButton.tag == 2){
        if (![self openLocationService]){
            YBAlertView *alertView = [[YBAlertView alloc] initWithTitle:@"提示" contentText:@"您的位置权限未开启,请到设置-Hearer-地理,开启位置权限?" leftButtonTitle:@"取消" rightButtonTitle:@"前往" leftBlock:^{
                
            } righBlock:^{
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    
                    //如果点击打开的话，需要记录当前的状态，从设置回到应用的时候会用到
                    
                    [[UIApplication sharedApplication] openURL:url];
                }
            }];
            [self closeView];
            [alertView show];
            return;
        }
    }
    
    
    [[NSUserDefaults standardUserDefaults] setInteger:self.tempSexButton.tag forKey:@"hear_selectSex"];
    [[NSUserDefaults standardUserDefaults] setInteger:[sortStr intValue] forKey:@"hear_selectSortType"];

    self.selectEnterBlock(@{@"Sex":sexStr,@"SortType":sortStr});
    [self closeView];
}


- (BOOL)openLocationService
{
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {
        return YES;
    }
    return NO;
}

- (void)recoveryButtonClick{
    
    
//    [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"hear_selectSex"];
    [[NSUserDefaults standardUserDefaults] setInteger:3 forKey:@"hear_selectSortType"];

    self.selectEnterBlock(@{@"Sex":@"2",@"SortType":@"1"});
    [self closeView];
}

- (void)likeButtonClick:(UIButton *)button{
    self.tempLikeButton.backgroundColor = [UIColor whiteColor];
    button.backgroundColor = CustomColor(240, 240, 240, 1);
    self.tempLikeButton = button;
    
}

- (void)sexButtonClick:(UIButton *)button{
    if(button.tag == self.tempSexButton.tag)return;
    button.backgroundColor = CustomColor(240, 240, 240, 1);
    self.tempSexButton.backgroundColor = [UIColor whiteColor];
    self.tempSexButton = button;

}

- (void)sortButtonClick:(UIButton *)button{
    if(button.tag == self.tempSortButton.tag)return;
    button.backgroundColor = CustomColor(240, 240, 240, 1);
    self.tempSortButton.backgroundColor = [UIColor whiteColor];
    self.tempSortButton = button;
}

-(void)closeView
{
    self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);

    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);

        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
